package com.edwin.websitebe.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edwin.websitebe.model.Member;
import com.edwin.websitebe.model.RedeemCode;
import com.edwin.websitebe.service.MemberService;
import com.edwin.websitebe.service.RedeemCodeService;
import com.edwin.websitebe.util.TokenUtil;
import com.edwin.websitebe.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/redeemCode")
@Slf4j
public class RedeemCodeController {

    @Autowired
    private RedeemCodeService redeemCodeService;

    @Autowired
    private MemberService memberService;

    @GetMapping("/list")
    public ResultVO list(@RequestParam(required = false) String code) {
        LambdaQueryWrapper<RedeemCode> wrapper = new QueryWrapper<RedeemCode>().lambda()
                .like(StringUtils.isNotBlank(code), RedeemCode::getCode, code)
                .orderByDesc(RedeemCode::getCreateTime);
        List<RedeemCode> list = redeemCodeService.list(wrapper);
        return new ResultVO().success(list);
    }

    @GetMapping("/delete")
    public ResultVO delete(@RequestParam Long id) {
        boolean removed = redeemCodeService.removeById(id);
        if (removed) {
            return new ResultVO().success();
        } else {
            return new ResultVO().fail("1", "删除失败，请重试");
        }

    }

    @GetMapping("/create")
    public ResultVO create(@RequestParam(required = false) Integer count) {
        if (Objects.isNull(count)) {
            count = 1;
        }
        ArrayList<RedeemCode> list = new ArrayList<>(count);
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < count; i++) {
            String uuid = IdUtil.simpleUUID();
            LambdaQueryWrapper<RedeemCode> wrapper = new QueryWrapper<RedeemCode>().lambda()
                    .eq(RedeemCode::getCode, uuid);
            RedeemCode redeemCode = redeemCodeService.getOne(wrapper);
            if (Objects.isNull(redeemCode)) {
                list.add(new RedeemCode(null, uuid, "0", now));
            }

        }
        boolean saved = redeemCodeService.saveBatch(list);
        if (saved) {
            return new ResultVO().success();
        } else {
            return new ResultVO().fail("1", "生成失败，请重试");
        }

    }

    @GetMapping("/redeem")
    public ResultVO redeem(@RequestParam String redeem, HttpServletRequest request) {
        if (StringUtils.isBlank(redeem)) {
            return new ResultVO().fail("1", "兑换码无效");
        }
        LambdaQueryWrapper<RedeemCode> wrapper = new QueryWrapper<RedeemCode>().lambda()
                .eq(RedeemCode::getCode, redeem)
                .eq(RedeemCode::getStatus, "0");
        RedeemCode redeemCode = redeemCodeService.getOne(wrapper);
        if (Objects.isNull(redeemCode)) {
            return new ResultVO().fail("1", "兑换码无效");
        }
        redeemCode.setStatus("1");
        redeemCodeService.updateById(redeemCode);

        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        Long memberId = TokenUtil.getUserId(token);
        Member member = new Member();
        member.setId(memberId);
        member.setIsMember(Boolean.TRUE);
        memberService.updateById(member);

        return new ResultVO().success();

    }

}
